package cn.dankal.flyaudio.adapter;

import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;

import com.kongzue.dialog.v2.SelectDialog;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.TabMainActivity;
import cn.dankal.flyaudio.database.FriendRecommendEntry;
import cn.dankal.flyaudio.dialog.AddFriendRequestSuccess;
import cn.dankal.flyaudio.jpush.JFriendManager;
import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.api.BasicCallback;

public class VerifyFriendAdapter extends BaseRecyclerViewAdapter<FriendRecommendEntry> {
    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_contract_person;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public class ViewHolder extends BaseRecyclerViewHolder<FriendRecommendEntry> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(FriendRecommendEntry data, int position) {
            setText(R.id.tv_name, data.nickName);
//            setVisibility(R.id.tv_agree,true);
            if (data.btnState == 1) {
                setText(R.id.tv_agree, "已接受");
            } else {
                setText(R.id.tv_agree, "接受");
            }
            itemView.findViewById(R.id.tv_agree).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data.btnState == 1) return;
                    JFriendManager.isMyFriend(data.username, new JFriendManager.GetMyFriendListener() {
                        @Override
                        public void isMyFriend(boolean isFriend) {
                            if (isFriend) {
                                ToastUtils.showShort("已经是好友，不需要重复添加！");
                            } else {
                                SelectDialog.show(context, "提示", "是否同意该好友申请!", "同意", (dialog, which) -> {
                                    ContactManager.acceptInvitation(data.username, FlyApplication.JPUSH_KEY, new BasicCallback() {
                                        @Override
                                        public void gotResult(int responseCode, String s) {
                                            if (0 == responseCode) {
                                                data.btnState = 1;
                                                notifyDataSetChanged();
                                                ToastUtils.showShort("已同意!");
                                            } else {
                                                ToastUtils.showShort(s);
                                            }
                                        }
                                    });
                                }, "取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                    }
                                });
                            }
                        }
                    });
                }
            });
        }
    }
}
